/*
 * MIT License
 *
 * Copyright (C) 2023 Huawei Device Co., Ltd.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

#ifndef FAST_IMAGEVIEW_COMPONENT_INSTANCE_H
#define FAST_IMAGEVIEW_COMPONENT_INSTANCE_H

#include "EventEmitters.h"
#include "FastImageNode.h"
#include "FastImageLoaderTurboModule.h"
#include "ShadowNodes.h"
#include "RNOH/CppComponentInstance.h"
#include "RNOH/arkui/ImageNode.h"

namespace rnoh {
class FastImageViewComponentInstance :
    public CppComponentInstance<facebook::react::FastImageViewShadowNode>,
    public FastImageNodeDelegate,
    public FastImageLoaderTurboModule::FastImageSourceResolver::ImageSourceUpdateListener {
private:
    FastImageNode m_imageNode;
    std::string m_uri;
    bool m_isReload = false;
    facebook::react::FastImageViewSourceStruct m_source;
    // used for find local cache of uri, if not find return uri
    std::string FindLocalCacheByUri(std::string const &uri);
    void GetHeaderUri(std::string const &uri,
                      std::vector<facebook::react::FastImageViewSourceHeadersStruct> const &header);
    std::string getBundlePath();
    std::string getAbsolutePathPrefix(std::string const &bundlePath);
    std::optional<std::string> getTintColorFromDynamic(folly::dynamic value);

public:
    explicit FastImageViewComponentInstance(Context context);
    void onPropsChanged(SharedConcreteProps const &props) override;
    //     void onStateChanged(SharedConcreteState const &state) override;

    void onComplete(float width, float height) override;
    void onProgress(uint32_t loaded, uint32_t total) override;
    void onError(int32_t errorCode) override;
    void onLoadStart();

    facebook::react::ImageResizeMode convertToImageResizeMode(facebook::react::FastImageViewResizeMode mode);

    FastImageNode &getLocalRootArkUINode() override;
    
    // ImageSourceResolver::ImageSourceUpdateListener
    void onImageSourceCacheUpdate(std::string fileUri) override;
    void onImageSourceCacheDownloadFileFail() override;
};
} // namespace rnoh

#endif // FAST_IMAGEVIEW_COMPONENT_INSTANCE_H
